<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row shadow="hover" v-show="showSearch" class="ml10">
				<el-form :inline="true" :model="state.queryForm" ref="queryRef" @keyup.enter="getDataList">
					<el-form-item :label="$t('ProdCategory.name')" prop="name">
						<el-input
							v-model="state.queryForm.name"
							:placeholder="$t('ProdCategory.inputNameTip')"
							clearable
							@clear="getDataList"
							style="max-width: 180px"
						/>
					</el-form-item>
					<el-form-item>
						<el-button @click="query" class="ml10" icon="search" type="primary">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button @click="resetQuery" icon="Refresh">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button @click="formDialogRef.openDialog()" class="ml10" icon="folder-add" type="primary" v-auth="'bizs_ProdCategory_add'">
						{{ $t('common.addBtn') }}
					</el-button>
					<el-button @click="handleExpand"> {{ $t('common.expandBtn') }} </el-button>
					<right-toolbar v-model:showSearch="showSearch" class="ml10" style="float: right; margin-right: 20px" @queryTable="getDataList">
					</right-toolbar>
				</div>
			</el-row>
			<el-table
				:defaultExpandAll="isExpand"
				ref="tableRef"
				:data="state.dataList"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				row-key="path"
				style="width: 100%"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle?.headerCellStyle"
			>
				<el-table-column :label="$t('ProdCategory.name')" fixed prop="name" show-overflow-tooltip></el-table-column>
				<el-table-column :label="$t('ProdCategory.icon')" prop="icon" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.icon" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('ProdCategory.pic')" prop="path" show-overflow-tooltip>
					<template #default="{ row }">
						<div class="flex-center-center">
							<ImageUpload v-model:imageUrl="row.pic" height="50px" width="50px" disabled />
						</div>
					</template>
				</el-table-column>

        <el-table-column :label="$t('ProdCategory.selectedPic')" prop="path" show-overflow-tooltip>
          <template #default="{ row }">
            <div class="flex-center-center">
              <ImageUpload v-model:imageUrl="row.selectedPic" height="50px" width="50px" disabled />
            </div>
          </template>
        </el-table-column>

				<el-table-column :label="$t('ProdCategory.sort')" prop="sort" show-overflow-tooltip></el-table-column>
				<el-table-column :label="$t('ProdCategory.isShow')" show-overflow-tooltip>
					<template #default="scope">
						<el-switch v-model="scope.row.isShow" @change="changeIsShow($event, scope.row)" />
					</template>
				</el-table-column>

				<el-table-column :label="$t('ProdCategory.path')" prop="path" show-overflow-tooltip></el-table-column>

				<el-table-column :label="$t('common.action')" show-overflow-tooltip width="250">
					<template #default="scope">
						<el-button icon="folder-add" @click="formDialogRef.openDialog('add', scope.row)" text type="primary" v-auth="'bizs_ProdCategory_add'">
							{{ $t('common.addBtn') }}
						</el-button>
						<el-button icon="edit-pen" @click="formDialogRef.openDialog('edit', scope.row)" text type="primary" v-auth="'bizs_ProdCategory_edit'"
							>{{ $t('common.editBtn') }}
						</el-button>

						<el-tooltip icon="delete" :content="$t('ProdCategory.deleteDisabledTip')" :disabled="!deleteDisabled(scope.row)" placement="top">
							<span style="margin-left: 12px">
								<el-button
									icon="delete"
									:disabled="deleteDisabled(scope.row)"
									@click="handleDelete(scope.row)"
									text
									type="primary"
									v-auth="'bizs_ProdCategory_del'"
								>
									{{ $t('common.delBtn') }}
								</el-button>
							</span>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList()" />
	</div>
</template>

<script setup lang="ts" name="systemProdCategory">
import { delObjs, getTree, putObj } from '/@/api/bizs/ProdCategory';
import { BasicTableProps, useTable } from '/@/hooks/table';
import { useMessage, useMessageBox } from '/@/hooks/message'; // 引入组件
// 定义i8n
import { useI18n } from 'vue-i18n'; // 定义查询字典
import { useDict } from '/@/hooks/dict'; // 引入组件
const ImageUpload = defineAsyncComponent(() => import('/@/components/Upload/Image.vue'));

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

const { t } = useI18n();

const { yes_no_type } = useDict('yes_no_type');
// 定义变量内容
const tableRef = ref();
const formDialogRef = ref();
const showSearch = ref(true);
const queryRef = ref();
const isExpand = ref(false);
const state: BasicTableProps = reactive<BasicTableProps>({
	pageList: getTree,
	queryForm: {
		name: '',
	},
	isPage: false,
});

const { getDataList, tableStyle } = useTable(state);

//是否禁用删除
const deleteDisabled = (row: any) => {
	return (row.children || []).length > 0;
};
// 展开折叠树
const handleExpand = async () => {
	isExpand.value = !isExpand.value;
	const dataList = await state.pageList();
	toggleExpand(dataList.data, isExpand.value);
};

const toggleExpand = (children: any[], unfold = true) => {
	for (const key in children) {
		tableRef.value?.toggleRowExpansion(children[key], unfold);
		if (children[key].children) {
			toggleExpand(children[key].children!, unfold);
		}
	}
};

const changeIsShow = async (e: any, row: any) => {
	let obj = {
		...row,
		isShow: e,
	};

	try {
		await putObj(obj);
		getDataList();
		useMessage().success(t('common.editSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

// 搜索事件
const query = () => {
	state.dataList = [];
	getDataList();
};

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	state.dataList = [];
	getDataList();
};

// 删除操作
const handleDelete = async (row: any) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObjs([row.id]);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
